﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LogManager.Base.Data
{
    public class DefaultEventService : IEventService, ITypedEventService, IServerEventService, ILogEventService
    {
        private string _typeId;
        private EventTypeStrategy _strategy;

        private string _serverName;
        private string _logName;

        private DateTime? _start;
        private DateTime? _end;
        private string _level;
        private string _user;

        private ILogService _logService;

        public DefaultEventService(ILogService logService)
        {
            _logService = logService;
        }

        #region IEventService members

        public ITypedEventService WithType(string typeId)
        {
            _strategy = EventTypeStrategy.Typed;
            _typeId = typeId;
            return this;
        }

        public ITypedEventService WithAnonymousType()
        {
            _strategy = EventTypeStrategy.Anonymous;
            return this;
        }

        public ITypedEventService WithSimpleType()
        {
            _strategy = EventTypeStrategy.Simple;
            return this;
        }

        #endregion

        #region ITypedEventService members

        public IServerEventService WithServer(string serverName)
        {
            _serverName = serverName;
            return this;
        }

        public IEnumerable<IEvent> All()
        {
            if(!string.IsNullOrEmpty(_serverName))
            {
                var logService = _logService.WithServer(_serverName);

                if(!string.IsNullOrEmpty(_logName))
                    return logService.Get(_logName).EventProvider.Filter(_start,_end,_level,_user).All();

                return logService.All().SelectMany(ls => ls.EventProvider.Filter(_start,_end,_level,_user).All());
            }

            return _logService.All().SelectMany(ls => ls.EventProvider.Filter(_start,_end,_level,_user).All());
        }

        public IQueryable<IEvent> Query()
        {
            if(!string.IsNullOrEmpty(_serverName))
            {
                var logService = _logService.WithServer(_serverName);

                if(!string.IsNullOrEmpty(_logName))
                    return logService.Get(_logName).EventProvider.Filter(_start,_end,_level,_user).All().AsQueryable();

                return logService.All().SelectMany(ls => ls.EventProvider.Filter(_start,_end,_level,_user).All()).AsQueryable();
            }

            return _logService.All().SelectMany(ls => ls.EventProvider.Filter(_start,_end,_level,_user).All()).AsQueryable();
        }

        #endregion

        #region IServerEventService members

        public ISelectableEventService Filter(DateTime? start = null, DateTime? end = null, string level = null, string user = null)
        {
            _start = start;
            _end = end;
            _level = level;
            _user = user;
            return this;
        }

        public ILogEventService WithLog(string logName)
        {
            _logName = logName;
            return this;
        }

        #endregion

        #region ILogEventService members

        public IEvent Get(string id)
        {
            return _logService.WithServer(_serverName).Get(_logName).EventProvider.Get(id);            
        }

        #endregion
    }
}
